package com.keshe.order.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.keshe.order.dao.OrderDao;
import com.keshe.order.dao.ReturnDao;
import com.keshe.order.entity.OrderEntity;
import com.keshe.order.entity.ReturnEntity;
import com.keshe.order.service.ReturnService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service("returnService")
public class ReturnServiceImpl extends ServiceImpl<ReturnDao, ReturnEntity> implements ReturnService {

    @Autowired
    ReturnDao returnDao;

    @Override
    public IPage<ReturnEntity> getReturnList(Map<String, Object> params) {
        long current = Long.valueOf((String) params.get("page")).longValue();
        long size = Long.valueOf((String) params.get("limit")).longValue();
        Page<OrderEntity> page = new Page<>(current, size);

        String returnSn = (String) params.get("returnSn");
        String orderSn = (String) params.get("orderSn");
        String account = (String) params.get("account");
        String returnStatus = (String) params.get("returnStatus");
        String applyTime = (String) params.get("applyTime");
        String handleTime = (String) params.get("handleTime");

        QueryWrapper<Map<String, Object>> wrapper = new QueryWrapper<>();
        wrapper
                .eq(StringUtils.isNotBlank(returnSn), "return_sn", returnSn)
                .eq(StringUtils.isNotBlank(orderSn), "order_sn", orderSn)
                .eq(StringUtils.isNotBlank(account), "account", account)
                .eq(StringUtils.isNotBlank(returnStatus), "return_status", returnStatus)
                .likeRight(StringUtils.isNotBlank(applyTime), "apply_time", applyTime)
                .likeRight(StringUtils.isNotBlank(handleTime), "handle_time", handleTime)
                .eq("t1.is_deleted", 0)
                .eq("t2.is_deleted", 0)
                .eq("t3.is_deleted", 0);

        return returnDao.getReturnList(page, wrapper);
    }

    @Override
    public ReturnEntity getReturnInfo(Integer returnId) {
        QueryWrapper<Map<String, Object>> wrapper = new QueryWrapper<>();
        wrapper.eq("t2.return_id", returnId);
        return returnDao.getReturnInfo(wrapper);
    }
}
